<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取行外style样式</title>
</head>
<body>
    <button id="btn">点击操作</button>
    <div id="boxid"></div>
    <script>
        document.write('<p style="color:red;"><br><br>获取非行内样式：<br>方法一：语法：getComputedStyle(元素对象).样式名;</p>');
        document.write('<p style="color:red;"><br>注：样式名中有下划线_时，需要去掉下划线，并将下划线后面的单词首字母大写，如：background_color应写成 backgroundColor。</p>');
        document.write('<p style="color:red;"><br>注：非行内样式的获取方法，只对 IE9 及以上的标准浏览器生效。</p>');
        document.write('<p style="color:red;"><br>方法二：对于 IE9 以下的浏览器的。</p>');
        document.write('<p style="color:red;"><br>元素对象.currentStyle.样式名。</p>');
        document.write('<p style="color:red;"><br>如：box.currentStyle.width;</p>');
        var eBtn = document.getElementById('btn');
        var box = document.getElementById('boxid');
        eBtn.onclick = function () {
            var box_W = getComputedStyle(box,null).width;
            var box_H = getComputedStyle(box).height;
            var box_nW = parseInt(box_W) + 20 + 'px';
            var box_nH = parseInt(box_H) + 20 + 'px';
            if (box.style.width >= '300px') {
                box.style.width = '300px';
                box.style.height = '300px';
            } else {
                box.style.width = box_nW;
                box.style.height = box_nH;
            }
        }
    </script>
    <style>
        * {
            margin: 0;
            padding: 0
        }
        #btn {
            width: 100px;
            height: 50px;
            border: 0;
            background-color: aqua;
            margin: 40px auto 20px auto;
            display: block;
        }
        #boxid {
            width:140px;
            height:140px;
            background-color: red;
            border: 0;
            margin: 0 auto;
        }
    </style>
</body>
</html>